﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;

namespace TestGame
{
    class TileMapManager
    {
        // keep track of all of the tile and map possibilities, but don't create any of them -- that takes resources..
        public static SortedDictionary<String, TileProto> tiles = new SortedDictionary<string,TileProto>();
        public static SortedDictionary<String, MapProto> maps = new SortedDictionary<string, MapProto>();

        public TileMapManager()
        {
            LoadTiles();
            LoadMaps();
        }

        private void LoadTiles()
        {
            // I wonder if there's a better way to do this..
            string[] fileEntries = Directory.GetFiles("../../../../TestGameContent/tiles");
            foreach (string fileName in fileEntries)
            {
                if (!fileName.EndsWith(".png")) { continue; }
                string[] split = fileName.Split('/');
                string tilepath = split[5].Split('.')[0];
                TileProto tp = new TileProto(tilepath);
                tiles.Add(tp.assetName, tp);
            }
        }

        private void LoadMaps()
        {
            string[] fileEntries = Directory.GetFiles("../../../../TestGameContent/maps");
            foreach (string fileName in fileEntries)
            {
                if (!fileName.EndsWith(".ktdm")) { continue; }
                StreamReader streamReader = new StreamReader(fileName);
                string file = streamReader.ReadToEnd();
                streamReader.Close();
                MapProto tp = new MapProto(file);
                maps.Add(tp.title, tp);
            }
        }
    }
}
